<?php

class User extends Morph_Object {
	
	public function __construct($id = null) {
		parent::__construct($id);		    
		$this->addProperty(new Morph_Property_String('userID'))
			 ->addProperty(new Morph_Property_String('hash'))
			 ->addProperty(new Morph_Property_String('username'))
			 ->addProperty(new Morph_Property_String('password'))
			 ->addProperty(new Morph_Property_String('firstName'))
			 ->addProperty(new Morph_Property_String('lastName'))
			 ->addProperty(new Morph_Property_String('street'))
			 ->addProperty(new Morph_Property_String('number'))
			 ->addProperty(new Morph_Property_String('zip'))
			 ->addProperty(new Morph_Property_String('city'))
			 ->addProperty(new Morph_Property_String('email'))
			 ->addProperty(new Morph_Property_HasMany('surveys', 'Survey'))
			 ->addProperty(new Morph_Property_HasMany('questionnaires', 'Questionnaire'));
	}
}

class Survey extends Morph_Object {

    public static $stati = array("active", "inactive", "archived", "incomplete", "stopped");
	public static $stati2 = array("active", "inactive", "archived", "paused");
	
	public function __construct($id = null) {
		parent::__construct($id);
        $this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_String('creator'))
			 ->addProperty(new Morph_Property_String('description'))
			 ->addProperty(new Morph_Property_String('validFrom'))
			 ->addProperty(new Morph_Property_String('validTo'))
			 ->addProperty(new Morph_Property_Boolean('periodic'))
			 ->addProperty(new Morph_Property_Integer('period'))
			 ->addProperty(new Morph_Property_String('periodUnit'))
			 ->addProperty(new Morph_Property_Integer('numberOfParticipants'))
			 ->addProperty(new Morph_Property_HasOne('questionnaireID', 'ActiveQuestionnaire'))
			 ->addProperty(new Morph_Property_Boolean('isPrivate'))
			 ->addProperty(new Morph_Property_Boolean('withKeys'))
			 ->addProperty(new Morph_Property_ComposeMany('keys', 'String'))
			 ->addProperty(new Morph_Property_String('status'))
			 ->addProperty(new Morph_Property_Boolean('editable'))
			 ->addProperty(new Morph_Property_Integer('numberOfParticipations'))
			 ->addProperty(new Morph_Property_String('privateLink'));
	}
	
	/**
	 * active:   not editable
	 * 		     stoppable
	 * 		     archiveable
	 * inactive: editable
	 * 			 stoppable
	 * 			 archiveable
	 * archived: not editable
	 * 			 not stoappable
	 * 			 unarchiveable (adjust status)
	 * stopped:  editable
	 * 			 unstoppable (adjust status)
	 * 			 archiveable
	 *
	 *
	 *
	 * editable: from creation to first active
	 * not editable: first time active
	 */
	
	public function isEditable() {
		return $this->status != "active"
			&& $this->status != "archived";
	}
	
	public function adjustStatus() {
		if(strtotime($this->validFrom) <= strtotime(date("d.m.Y"))
			&& strtotime($this->validTo) > strtotime(date("d.m.Y"))) {
			$this->status = "active";
		} else {
			$this->status = "inactive";
		}
	}
}

class Questionnaire extends Morph_Object {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_String('description'))
			 ->addProperty(new Morph_Property_HasMany('questionBlocks', 'QuestionBlock'));
	}
}

class ActiveQuestionnaire extends Questionnaire {
	
	public function __construct($id = null) {
		parent::__construct($id);
	}
}

class QuestionBlock extends Morph_Object {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_HasMany('singleChoiceQuestions', 'SingleChoiceQuestion'))
			 ->addProperty(new Morph_Property_HasMany('singleChoiceMatrixQuestions', 'SingleChoiceMatrixQuestion'))
			 ->addProperty(new Morph_Property_HasMany('textQuestions', 'TextQuestion'));
	}
}

class SingleChoiceQuestion extends Question {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_ComposeMany('selectableAnswers', 'SelectableAnswer'))
			 ->addProperty(new Morph_Property_Integer('questionIndex'));
	}
}

class SingleChoiceMatrixQuestion extends Question {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_HasMany('singleChoiceQuestions', 'SingleChoiceQuestion'))
			 ->addProperty(new Morph_Property_Integer('questionIndex'));
	}
}

class TextQuestion extends Question {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_ComposeMany('answers', 'String'))
			 ->addProperty(new Morph_Property_Integer('questionIndex'));
	}
}

class SelectableAnswer extends Morph_Object {
	
	public function __construct($id = null) {
		parent::__construct($id);
		$this->addProperty(new Morph_Property_String('title'))
			 ->addProperty(new Morph_Property_Integer('selections'))
			 ->addProperty(new Morph_Property_Boolean('selected'));
	}
}
?>
